var database = require('../../../../data/data.js')
Page({

  data: {
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false,
    imgChange: false,
    addressChange: false
  },
  bindChange: function (e) {
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;
    if (val[0] != t[0]) {
      const citys = [];
      const countys = [];
      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        if (cityData[val[0]].sub[0].sub[i] != null) {
          countys.push(cityData[val[0]].sub[0].sub[i].name)
        }
      }
      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0],
        addressChange: true
      })
      return;
    }
    if (val[1] != t[1]) {
      const countys = [];
      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        if (cityData[val[0]].sub[val[1]].sub[i] != null) {
          countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
        }
      }
      if (cityData[val[0]].sub[val[1]].sub[0] != null) {
        this.setData({
          city: this.data.citys[val[1]],
          county: cityData[val[0]].sub[val[1]].sub[0].name,
          countys: countys,
          values: val,
          value: [val[0], val[1], 0],
          addressChange: true
        })
      } else {
        this.setData({
          city: this.data.citys[val[1]],
          county: "",
          countys: countys,
          values: val,
          value: [val[0], val[1], 0],
          addressChange: true
        })
      }
      return;
    }
    if (val[2] != t[2]) {
      this.setData({
        county: this.data.countys[val[2]],
        values: val,
        addressChange: true
      })
      return;
    }
  },
  open: function () {
    this.setData({
      condition: !this.data.condition
    })
  },
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    this.setData({
      softballId: id
    })
    wx.request({
      url: database.url1 + '/api/V2/softball/get_softball',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        accessToken: database.accessToken,
        userId: database.userId,
        softballId: id
      },
      success(res) {
        that.setData({
          teamDetail: res.data.data,
          provinceId: res.data.data.provinceId,
          cityId: res.data.data.cityId,
          countyId: res.data.data.countyId,
          province: res.data.data.proviceName,
          city: res.data.data.cityName,
          county: res.data.data.countyName,
          img: res.data.data.logoUrl,
          shortName: res.data.data.shortName,
          title: res.data.data.title
        })
      }
    })
    wx.request({
      url: database.url1 + '/api/V2/mask/create_code_infinite',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        accessToken: database.accessToken,
        userId: database.userId
      },
      success(res) {
        console.log(res)
        that.setData({
          soleCode: res.data.data.soleCode
        })
      }
    })
    wx.request({
      url: database.url1 + '/api/V2/position/address_map',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        accessToken: database.accessToken
      },
      success(res) {
        var cityData = res.data.data;
        const provinces = [];
        const citys = [];
        const countys = [];
        for (let i = 0; i < cityData.length; i++) {
          provinces.push(cityData[i].name);
        }
        for (let i = 0; i < cityData[0].sub.length; i++) {
          citys.push(cityData[0].sub[i].name)
        }
        for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
          if (cityData[0].sub[0].sub[i] != null) {
            countys.push(cityData[0].sub[0].sub[i].name)
          }
        }
        that.setData({
          'cityData': cityData,
          'provinces': provinces,
          'citys': citys,
          'countys': countys
        })
      }
    })
  },
  chooseImage: function () {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        if (res.tempFiles[0].size < 5242880) {
          _this.setData({
            finish1: true,
            img: res.tempFilePaths[0],
            imgChange: true
          })
        } else {
          wx.showModal({
            content: '选择的的图片文件太大，请重新选择',
          })
        }
      }
    })
  },
  nameChange: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  shortNameChange: function (e) {
    this.setData({
      shortName: e.detail.value
    })
  },
  submit: function (e) {
    wx.showLoading({
      title: '修改中',
      mask:true
    })
    var that = this;
    if (that.data.shortName != "" && that.data.title != "") {
      if (this.data.addressChange) {
        var citysData = this.data.cityData;
        var province_count = this.data.value[0];
        var city_count = this.data.value[1];
        var county_count = this.data.value[2];
        var province_code = citysData[province_count].code;
        var city_code = citysData[province_count].sub[city_count].code;
        var county_code = citysData[province_count].sub[city_count].sub[county_count].code;
      } else {
        var province_code = this.data.provinceId;
        var city_code = this.data.cityId;
        var county_code = this.data.countyId;
      }
      wx.request({
        url: 'https://51.5izuqiu.cn/api/V2/softball/save_softball',
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          accessToken: database.accessToken,
          soleCode: that.data.soleCode,
          userId: database.userId,
          softballId: that.data.softballId,
          title: that.data.title,
          shortName: that.data.shortName,
          provinceId: province_code,
          cityId: city_code,
          countyId: county_code
        },
        success(res) {
          wx.request({
            url: database.url+'/project/uploadUpdataTeam.action',
            data:{
              openid:database.userId,
              teamid: that.data.softballId
            },
            success(res){
              
            }
          })
          if (that.data.imgChange) {
            wx.uploadFile({
              url: 'https://51.5izuqiu.cn/api/V2/softball/upload_softball_logo',
              filePath: that.data.img,
              name: 'logoUrl',
              formData: {
                accessToken: database.accessToken,
                userId: database.userId,
                softballId: that.data.softballId,
              },
              success(res) {
                wx.hideLoading();
                wx.navigateBack({
                  delta: 2,
                })
              }
            })
          } else {
            wx.hideLoading();
            wx.navigateBack({
              delta: 2,
            })
          }
        }
      })
    }
  }
})